package com.catmai.pssmanager.dao.impl;

import java.sql.SQLException;
import java.util.List;

import com.catmai.pssmanager.dao.StoreHouseDao;
import com.catmai.pssmanager.po.Storehouse;

public class StoreHouseDaoImpl extends BasicDaoImpl<Storehouse> implements StoreHouseDao{

	@Override
	public List<Storehouse> findAll() throws SQLException {
		String sql = "select s.storehouse_id,s.storehouse_address,s.storehouse_capacity,s.storehouse_stock from storehouse s";
		List<Storehouse> list = this.getList(connection, sql);
		return list;
	}

	@Override
	public void saveObject(Storehouse storehouse) throws SQLException {
		String sql = "insert into storehouse values(?,?,?,?)";
		Object [] args = {storehouse.getStorehouse_Id(),storehouse.getStorehouse_Address(),storehouse.getStorehouse_Capacity(),storehouse.getStorehouse_Stock()};
		this.update(connection, sql, args);
	}

	@Override
	public void deleteObject(Integer storehouseId) throws SQLException {
		String sql = "delete from storehouse where storehouse_id = ?";
		this.update(connection, sql, storehouseId);
	}

	@Override
	public void updateObject(Storehouse storehouse) throws SQLException {
		String sql = "update storehouse set storehouse_address=?,storehouse_capacity=?,storehouse_stock=? where storehouse_id=?";
		Object [] args = {storehouse.getStorehouse_Address(),storehouse.getStorehouse_Capacity(),storehouse.getStorehouse_Stock(),storehouse.getStorehouse_Id()};
		this.update(connection, sql, args);
	}

	@Override
	public Storehouse getStorehouseById(Integer storeHouseId) throws SQLException {
		String sql = "select s.storehouse_id,s.storehouse_address,s.storehouse_capacity,s.storehouse_stock from storehouse s where storehouse_id=?";
		return this.get(connection, sql, storeHouseId);
	}

	@Override
	public void updateStock(Integer storeNumber,Integer storehouseId) throws SQLException {
		String sql = "update storehouse set storehouse_stock = storehouse_stock + ? where storehouse_id = ?";
		Object [] args = {storeNumber,storehouseId};
		this.update(connection, sql, args);
	}

}
